
public class Solution2 {

    public ListNode mergeTwoLists1(ListNode list1, ListNode list2) {
        ListNode newhead = new ListNode(-1);
        ListNode temp = newhead;
        while(list1 != null && list2 != null) {
            if(list1.val < list2.val) {
                temp.next = list1;
                list1 = list1.next;
                temp = temp.next;
            }else {
                temp.next = list2;
                list2 = list2.next ;
                temp = temp.next;
            }
        }
        if(list1 != null) {
            temp.next = list1;
        }
        if(list2 != null) {
            temp.next = list2;
        }
        return newhead.next;
    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null) {
            return list2;
        }else if(list2 == null) {
            return list1;
        }

        if(list1.val <= list2.val) {
            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        }else {
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }
}
